﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Windows.Forms;

namespace zUfDsl.Common
{
    public class HandleButton
    {
        public delegate void syncproc();

        #region 异步执行按钮
        /// <summary>
        /// 异步执行按钮
        /// </summary>
        public static void SyncButton(ToolStripButton bt, string caption, ref Models.tipConfig tipconfig, syncproc sync)
        {
            try
            {
                bt.Enabled = false;
                bt.Text = string.Format("{0}中...", caption);
                SyncButtonProc(ref tipconfig, sync);
            }
            catch (Exception ex)
            {
                var fmsg = new FrmMessage();
                fmsg.ShowMsg(false, "出现错误", $"{ex.Message}---{ex.StackTrace}");
            }
            finally
            {
                bt.Enabled = true;
                bt.Text = caption;
            }

        }
        #endregion

        #region 异步执行按钮
        /// <summary>
        /// 异步执行按钮
        /// </summary>
        public static void SyncButton(Button bt, string caption, ref Models.tipConfig tipconfig, syncproc sync)
        {
            try
            {
                bt.Enabled = false;
                bt.Text = string.Format("{0}中...", caption);
                SyncButtonProc(ref tipconfig, sync);
            }
            catch (Exception ex)
            {
                var fmsg = new FrmMessage();
                fmsg.ShowMsg(false, "出现错误", $"{ex.Message}---{ex.StackTrace}");
            }
            finally
            {
                bt.Enabled = true;
                bt.Text = caption;
            }

        }
        #endregion

        #region 异步执行按钮
        /// <summary>
        /// 异步执行按钮
        /// </summary>
        private static void SyncButtonProc(ref Models.tipConfig tipconfig, syncproc sync)
        {
            try
            {
                var ftip = new FrmTip();
                ftip.Show();
                tipconfig = new Models.tipConfig() { tip = "正在从服务器查询数据" };
                ftip.SetValue(tipconfig);
                Application.DoEvents();
                System.Threading.Thread.Sleep(500);

                var threadstart = new ThreadStart(sync);
                var task = new Thread(threadstart);
                task.Start();
                //int i = 0;
                while (task.ThreadState != ThreadState.Stopped)
                {
                    ftip.SetValue(tipconfig);
                    Application.DoEvents();
                    System.Threading.Thread.Sleep(100);
                }
                if (ftip != null) { ftip.Close(); ftip.Dispose(); }
                if (tipconfig.cMsg.ObjToString().Length > 0)
                {
                    if (tipconfig.cCaption.ObjToString().Length == 0)
                    {
                        tipconfig.cCaption = "提醒";
                    }
                    var fmsg = new FrmMessage();
                    if (tipconfig.bSuc)
                    {
                        fmsg.ShowMsg(tipconfig.bSuc, tipconfig.cCaption, tipconfig.cMsg);
                    }
                    else
                    {
                        fmsg.ShowMsg(tipconfig.bSuc, tipconfig.cCaption, $"{tipconfig.cMsg}--【技术信息{tipconfig.tipm}--{tipconfig.tip}】");
                    }
                }
            }
            catch (Exception ex)
            {
                var fmsg = new FrmMessage();
                fmsg.ShowMsg(false, "出现错误", $"{ex.Message}---{ex.StackTrace}");
            }

        }
        #endregion 

    }
}
